// pages/setpaw/setpaw.js
import WxValidate from '../../utils/WxValidate.js';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      r:0,//1是订单支付时过来的2是密码管理过来的
      orderid:'',//订单支付时的订单号
    buttonText: '短信验证码',
    disabled: false,
    vericode: '',
    paw: '',
    cfmpaw: '',
    userInfo:null,
    haspaw:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this;
    if (options.r==1){
      this.setData({r:options.r,orderid:options.orderid})
        wx.setNavigationBarTitle({
          title: '设置支付密码',
        })
      }
    this.initValidate()
    //获取个人信息
    wx.request({
      url: app.globalData.reqPath + 'shop/Members',
      data: {},
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (data.code == 200) {
          if (data.data.tradePasswordSet==1){
            self.setData({ haspaw:true})
          }else{
            self.setData({ haspaw: false })
          }
          self.setData({ userInfo: data.data })
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getcode: function () {
    let userInfo = this.data.userInfo;
    let self = this;
    let num = 61;
    self.setData({
      disabled: false
    })
    var timer = setInterval(function () {
      num--;
      if (num <= 0) {
        clearInterval(timer);
        self.setData({
          buttonText: '重新发送',
          disabled: false
        })
      } else {
        self.setData({
          buttonText: num + "s后重新发送",
          disabled: true
        })
      }
    }, 1000)
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersSms',
      header: {
        'authorization': app.globalData.authorization
      },
      data: {
        phone: userInfo.phone
      },
      method: "POST",
      success: function ({ data, header }) {
        // console.log(res.data);
        app.globalData.authorization=header.authorization||header.Authorization;
        wx.setStorage({
          key: 'authorization',
          data: header.authorization || header.Authorization,
        })
       
      }
    })
  },
  //表单验证
  initValidate() {
    const rules = {
      paw: {
        required: true,
        minlength: 6
      },
      cfmpaw: {
        required: true,
        equalTo: 'paw'
      },
      vericode: {
        required: true,
        number: true,
        maxlength: 8
      }
    }
    const messages = {
      paw: {
        required: '请输入密码',
        minlength: "密码为6位"
      },
      cfmpaw: {
        required: '确认密码不能为空',
        equalTo: '两次输入不同'
      },
      vericode: {
        required: "请输入验证码",
        number: "请输入正确的验证码",
        maxlength: '请输入正确的验证码'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  showModal: function (error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  subset: function (e) {
    let parames = e.detail.value;
    let self = this;
    console.log(parames)
    if (!this.WxValidate.checkForm(parames)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false;
    }

    wx.request({
      url: app.globalData.reqPath + 'shop/MembersSafe',
      method: 'POST',
      data: {
        newTradePassword: parames.cfmpaw,
        code: parames.vericode
      },
      header: {
        'authorization': app.globalData.authorization
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '设置成功',
            icon: 'none'
          })
          self.getMebers();//更新个人信息

        setTimeout(function(){
          if (self.data.r == 1) {
            wx.redirectTo({
              url: '/pages/orderDetail/orderDetail?id=' + self.data.orderid,
            })
          } else {
            wx.navigateBack({})
          }
        },300)

        } else {
          wx.showToast({
            title: res.data.data.msg,
            icon: 'none',
          })
        }
      }
    })
  },
  inputpaw: function (e) {
    this.setData({ paw: e.detail.value })
  },
  inputcfmpaw: function (e) {
    this.setData({ cfmpaw: e.detail.value })
  },
  inputveri: function (e) {
    this.setData({ vericode: e.detail.value })
  },
  getMebers:function(){
    wx.request({
      url: app.globalData.reqPath + 'shop/Members',
      data: {},
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (data.code == 200) {
          
          //self.setData({ userInfo: data.data })
          wx.setStorage({
            key: 'userInfo',
            data: data.data,
          })
        }
      }
    })
  }
})